# -*- coding: utf-8 -*-
from app import db
import datetime

__author__ = 'nibo'


class User(db.Model):
    """
    用户表
    """
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(64))
    # 学习记录
    note = db.relationship('Note', backref='author', lazy='dynamic')
    # 学习复习记录
    note_review = db.relationship('NoteReview', backref='author', lazy='dynamic')

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def __repr__(self):
        return '<User %s>' % self.email


class ReviewSetting(db.Model):
    """
    复习设置表
    """
    id = db.Column(db.Integer, primary_key=True)
    # 次数
    time = db.Column(db.Integer)
    # 间隔天数
    day = db.Column(db.Integer)

    def __repr__(self):
        return '<ReviewSetting id:%s, time:%s, day:%s>' % (self.id, self.time, self.day)


class Note(db.Model):
    """
    学习记录表
    """
    id = db.Column(db.Integer, primary_key=True)
    # 内容
    content = db.Column(db.String(120))
    # 备注
    remark = db.Column(db.String(140))
    # 记录时间
    note_time = db.Column(db.DateTime)
    # 操作时间，默认为系统时间
    timestamp = db.Column(db.DateTime)
    # 用户id
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    # 学习记录复习
    note_review = db.relationship('NoteReview', backref='note', lazy='dynamic')

    def __repr__(self):
        return '<Note id:%s, content:%s>' % (self.id, self.content.encode('utf-8'))


class NoteReview(db.Model):
    """
    学习记录复习表
    """
    id = db.Column(db.Integer, primary_key=True)
    # 复习时间
    review_time = db.Column(db.DateTime)
    # 复习次数
    review_num = db.Column(db.Integer)
    # 原始笔记，0:不是，1:是
    original_note = db.Column(db.SmallInteger)
    note_id = db.Column(db.Integer, db.ForeignKey('note.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __repr__(self):
        return '<NoteReview id:%s>' % self.id
